<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>客服</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    /*.back {
        display: inline-block;
        width: 15%;
        height: 50px;
        line-height: 50px;
        position: absolute;
        top: 0;
        left: 0;
        text-align: center;
        z-index: 1;
    }

    .top {
        background: #E5E5E5;
        width: 100%;
        height: 50px;
        position: absolute;
        top: 0;
    }

    .top .nickname {
        width: 50%;
        margin: 0 auto;
        text-align: center;
        height: 50px;
        line-height: 50px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        font-weight: bold;
    }*/

    .content {
        width: 100%;
        position: absolute;
        top: 0;
        bottom: 90px;
        border-top: 1px solid rgb(224, 224, 224);
        border-bottom: 1px solid rgb(224, 224, 224);
        background: rgb(232, 233, 232);
        overflow: auto;
    }

    .content .obj {
        max-width: calc(100% - 60px);
        margin-top: 10px;
    }

    .content .obj .avatar {
        margin-left: 10px;
        width: 40px;
        height: 40px;
        object-fit: cover;
        border-radius: 5px;
        float: left;
    }

    .content .obj .msg {
        max-width: calc(100% - 80px);
        min-height: 20px;
        background: #fff;
        margin-left: 10px;
        padding: 10px;
        border-radius: 5px;
        float: left;
    }

    .content .me {
        max-width: 100%;
        margin-top: 10px;
    }

    .content .me .avatar {
        width: 40px;
        height: 40px;
        object-fit: cover;
        border-radius: 5px;
        float: right;
        margin: 0 10px;
    }

    .content .me .msg {
        min-height: 20px;
        background: rgb(134, 236, 87);
        margin-left: 60px;
        padding: 10px;
        border-radius: 5px;
        float: right;
        max-width: calc(100% - 140px);
    }

    .footer {
        background: #E5E5E5;
        width: 100%;
        min-height: 50px;
        max-height: 300px;
        position: absolute;
        bottom: 0;
    }

    .end-contact {
        width: 96%;
        height: 30px;
        margin-left: 1.5%;
        border: 0;
        background-color: #FA5151;
        color: #fff;
        border-radius: 5px;
        margin-top: 10px;
    }

    .echo {
        width: 74%;
        min-height: 30px;
        max-height: 250px;
        margin-left: 1.5%;
        margin-top: 10px;
        border: 0;
        border-radius: 5px;
        resize: none;
        float: left;
        background-color: #fff;
        overflow-y: scroll;
        margin-bottom: 10px;
    }

    div:focus {
        outline: none;
    }

    .send {
        width: 20%;
        height: 30px;
        margin-left: 1.5%;
        margin-top: 10px;
        text-align: center;
        background-color: #07c160;
        color: #fff;
        border: 0;
        border-radius: 5px;
    }

    .end {
        width: 100%;
        text-align: center;
        height: 20px;
        padding: 10px 0;
        color: #999;
    }

    .will_over_time {
        width: 100%;
        text-align: center;
        height: 20px;
        padding: 10px 0;
        color: #999;
    }

    /* 去除滚动条样式 */
    ::-webkit-scrollbar {
        display: none; /* Chrome Safari */
    }
</style>
<body>

<!--<div class="back">返回</div>-->
<!--<div class="top">
    &lt;!&ndash;    <div class="nickname">{$contact_username}</div>&ndash;&gt;
    <div class="nickname">客服</div>
</div>-->
<!--  内容显示区域  -->
<div class="content">
    {volist name="msg_list" id="vo"}
    <div class="{$vo.from_uid == $uid ? 'me' : 'obj'}">
        <img class="avatar" src="{$vo.from_avatar}"/>
        <div class="msg">
            {$vo.msg}
        </div>
    </div>
    <div style="clear: both"></div>
    {/volist}
</div>
<!--  底部  -->
<div class="footer">
    <button class="end-contact">结束客服</button>
    <div class="echo" contenteditable="true"></div>
    <button class="send">发送</button>
</div>

</body>
<script src="__STATIC__/jquery/jquery-3.1.1.min.js"></script>
<script src="/static/layer/mobile/layer.js"></script>
<script>
    var user_info = JSON.parse(`{$user_info}`);

    // 将聊天框的滚动条滚动到底部
    scrollBottom();

    var messages = [];
    ws = new WebSocket("ws://worker.lizaonet.com:2346");
    ws.onopen = function () {
        /*var data = {
            "type": "user_connect",
            "user_id": user_info.id
        }
        ws.send(JSON.stringify(data));*/
    };

    ws.onmessage = function (e) {
        console.log(e.data)
        if (isJSON(e.data)) {
            var data = JSON.parse(e.data);

            switch (data.type) {
                case 'close':
                    // 连接断开
                    console.log('连接已断开', user_info.id);
                    $('.send').prop("disabled", true).css('background', '#999');
                    $('.end-contact').prop("disabled", true).css('background', '#999');
                    $('.echo').css({"pointer-events": "none"});

                    // 关闭连接并通知客服
                    closeThenNotice(user_info.id);

                    break;
                case 'contact_send':
                    // 接收到客服发送的消息
                    var get_msg = {
                        type: 'obj',
                        avatar: data.avatar,
                        msg: data.msg
                    };
                    pushMsg(get_msg);
                    break;
                case 'will_over_time':
                    // 即将结束通话提醒
                    pushWillOverTime(data.msg);
                    scrollBottom();
                    break;
                default:
                    console.log('default')
            }
        } else {
            console.log(e)
        }
    };

    function isJSON(str) {
        if (typeof str == 'string') {
            try {
                var obj = JSON.parse(str);
                if (typeof obj == 'object' && obj) {
                    return true;
                } else {
                    return false;
                }
            } catch (e) {
                return false;
            }
        }
    }

    $('.send').click(function () {
        sendMessage();
    })

    /**
     * 关闭连接并通知客服
     * @param int user_id 用户id
     * */
    function closeThenNotice(user_id) {
        var data = {
            type: "user_close",
            user_id: user_id
        }
        ws.send(JSON.stringify(data));

        // 关闭连接
        ws.close();

        pushEnd('本次通话已结束');
    }

    $('.end-contact').click(function () {
        layer.open({
            content: '您确定要结束客服吗？'
            , btn: ['结束', '取消']
            , yes: function (index) {
                // 关闭连接并通知客服
                closeThenNotice(user_info.id);

                $('.send').prop("disabled", true).css('background', '#999');
                $('.end-contact').prop("disabled", true).css('background', '#999');
                $('.echo').css({"pointer-events": "none"});
                layer.close(index);
            }
        });
    })

    /*$(document).keydown(function (event) {
        // 禁止回车换行
        event.cancelBubble = true;
        event.preventDefault();
        event.stopPropagation();

        var btn_status = $('.send').attr('disabled');
        if (event.keyCode == 13 && btn_status != 'disabled') {
            sendMessage();
        }
    });*/

    // 发送消息
    function sendMessage() {
        var msg = $('.echo').html();

        if (msg !== '' && msg != undefined) {
            var data = {
                type: "user_send",
                user_id: user_info.id,
                username: user_info.username,
                avatar: user_info.avatar,
                msg: msg
            }
            ws.send(JSON.stringify(data));

            $('.echo').html('');

            data.type = 'me';
            data.avatar = user_info.avatar;
            pushMsg(data);
        }
    }

    // 将聊天框的滚动条滚动到底部
    function scrollBottom() {
        $('.content').scrollTop($('.content').prop("scrollHeight"));
    }

    // 渲染消息数据
    function pushMsg(data) {
        var html = '<div class="' + data.type + '">\n' +
          '        <img class="avatar" src="' + data.avatar + '"/>\n' +
          '        <div class="msg">\n' +
          '            ' + data.msg + '\n' +
          '        </div>\n' +
          '    </div>\n' +
          '    <div style="clear: both"></div>';
        $('.content').append(html);

        scrollBottom();
    }

    // 长时间未通信提醒
    function pushWillOverTime(msg) {
        var html = '<div class="will_over_time">' + msg + '</div>';
        $('.content').append(html);
        scrollBottom();
    }

    // 通话结束提醒
    function pushEnd(msg) {
        var html = '<div class="end">' + msg + '</div>';
        $('.content').append(html);
        scrollBottom();

        // 标识聊天记录结束
        $.post('/push/chat/msgEnd', {uid: user_info.id})
    }

</script>
</html>